home *** CD-ROM | disk | FTP | other *** search
/ PC Users 1999 May / Cd Pc Users extra 20 mayo 1999.iso / Prog / Inst / FTP / WINFTP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-21  |  7.2 KB  |  272 lines

  1. /*
  2. **  WINFTP.C
  3. **
  4. **  WINFTP Client.
  5. **
  6. **  Last modified: 3/14/1999
  7. */
  8.  
  9. #include <windows.h>
  10. #include <winsock.h>
  11. #include "fce.h"
  12. #include "connect.h"
  13. #include "files.h"
  14. #include "info.h"
  15. #include "message.h"
  16.  
  17. #ifdef WIN32
  18. #define USE_INS HINSTANCE
  19. #define USE_PTR PSTR
  20. #else
  21. #define USE_INS HANDLE
  22. #define USE_PTR LPSTR
  23. #endif
  24.  
  25. #define BS          8
  26. #define LF         10
  27. #define CR         13
  28. #define ESC        27
  29.  
  30. #define MAX_BUF   2048
  31. #define RX_BUF    MAX_BUF
  32. #define TX_BUF    512
  33. #define MAX_STR   64
  34.  
  35. #define POPWIDTH  140
  36. #define POPHEIGHT  50
  37.  
  38. /* private */
  39.  
  40. static USE_INS hInstance;
  41. static RECT MainRect;
  42. static char Temp[MAX_STR+8];   
  43.  
  44. /* globals */
  45.  
  46. HWND hMainWnd;            /* main window handle */
  47. HWND hInfoWnd;            /* popup handle */            
  48. HCURSOR ArrowCursor;      /* arrow cursor */
  49. HCURSOR WaitCursor;       /* hour glass cursor */
  50. int ConnectStatus = FALSE;
  51. int MasterAbort = FALSE;
  52.  
  53. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM); 
  54.  
  55. void Message(LPSTR Message) 
  56. {MessageBox(NULL,Message,(LPSTR)"INFO", MB_TASKMODAL|MB_ICONEXCLAMATION);
  57. }
  58.  
  59. int AskUser(LPSTR Message) 
  60. {return MessageBox(NULL,Message,(LPSTR)"CONFIRM",
  61.               MB_TASKMODAL|MB_ICONQUESTION|MB_OKCANCEL);
  62. }
  63.  
  64. void ShowError(int Code)
  65. {char Temp1[80]; 
  66.  char Temp2[100];
  67.  fceErrorText(0,Code,(LPSTR)Temp1,80);
  68.  wsprintf((LPSTR)Temp2, "ERROR %d: %s", Code, Temp1);
  69.  Message((LPSTR)Temp2);
  70.  SetCursor(ArrowCursor);
  71. }
  72.  
  73. int IsConnected(void)
  74. {if(fceGetInteger(0,FCE_GET_CONNECT_STATUS)) return TRUE;
  75.  Message("Connection to FTP server lost");
  76.  return FALSE;
  77. }
  78.  
  79. /* WinMain */
  80.  
  81. #ifdef WIN32
  82. int WINAPI
  83. #else
  84. int PASCAL
  85. #endif
  86. WinMain(USE_INS hInst, USE_INS hPrevInstance,
  87.         USE_PTR szCmdLine,  int nCmdShow)
  88. {WNDCLASS  wc1, wc2;
  89.  MSG msg;
  90.  BOOL Result;
  91.  if(!hPrevInstance)
  92.    {/* register main window class */
  93.     wc1.style = CS_HREDRAW | CS_VREDRAW;
  94.     wc1.lpfnWndProc = MainWndProc;
  95.     wc1.cbClsExtra = 0;
  96.     wc1.cbWndExtra = 0;
  97.     wc1.hInstance = hInst;
  98.     wc1.hIcon = LoadIcon(hInst, "HostIcon");
  99.     wc1.hCursor = NULL;
  100.     wc1.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  101.     wc1.lpszMenuName =  "HostMenu";
  102.     wc1.lpszClassName = "HostWClass";
  103.     Result = RegisterClass(&wc1);
  104.     if(!Result) return FALSE;
  105.  
  106.     /* register popup window class */
  107.     wc2.style = CS_HREDRAW | CS_VREDRAW | CS_PARENTDC;
  108.     wc2.lpfnWndProc = InfoWndProc;
  109.     wc2.cbClsExtra = 0;
  110.     wc2.cbWndExtra = 0;
  111.     wc2.hInstance = hInst;
  112.     wc2.hIcon = NULL;
  113.     wc2.hCursor = LoadCursor(NULL, IDC_ARROW);
  114.     wc2.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  115.     wc2.lpszMenuName =  NULL;
  116.     wc2.lpszClassName = "InfoWClass";
  117.     Result = RegisterClass(&wc2);
  118.     if(!Result) return FALSE;
  119.    }
  120.  /* create main window */
  121.  hInstance = hInst;
  122.  hMainWnd = CreateWindow(
  123.         "HostWClass",   "WINFTP",  WS_OVERLAPPEDWINDOW,
  124.         CW_USEDEFAULT,  CW_USEDEFAULT,
  125.         250,            130,            
  126.         NULL,           NULL,
  127.         hInstance,      NULL);
  128.  ShowWindow(hMainWnd, nCmdShow);
  129.  UpdateWindow(hMainWnd);
  130.  
  131.  /* window control loop */
  132.  
  133.  while(GetMessage(&msg,NULL,0,0))
  134.    {
  135.     TranslateMessage(&msg);
  136.     DispatchMessage(&msg);
  137.    }
  138.  return (msg.wParam);
  139. } /* end WinMain */
  140.  
  141. #ifdef WIN32
  142. LRESULT CALLBACK
  143. #else
  144. long FAR PASCAL
  145. #endif
  146. MainWndProc(HWND hWindow,UINT iMsg,WPARAM wParam,LPARAM lParam)
  147. {int Code;
  148.  HMENU hMenu;
  149.  static int Version;
  150.  HDC hDC;
  151.  PAINTSTRUCT ps;
  152. #ifdef WIN32
  153. #else
  154.  static FARPROC lpfnConnectDlgProc;
  155.  static FARPROC lpfnFilesDlgProc;
  156. #endif
  157.  static char Msg1[] = "Welcome to WINFTP";
  158.  static char Msg2[] = "Choose 'Connection' to connect.";
  159.  static char Msg3[] = "Choose 'Files' after connecting.";
  160.  /* begin */
  161.  hMainWnd = hWindow;
  162.  switch (iMsg)
  163.     {case WM_CREATE:
  164.        /* create popup Info window */
  165.        GetWindowRect(hMainWnd,&MainRect);
  166.        hInfoWnd = CreateWindow(
  167.           "InfoWClass", "Info",
  168.           WS_POPUP | WS_BORDER | WS_CAPTION | WS_THICKFRAME,
  169.           12,  12,
  170.           POPWIDTH, POPHEIGHT,
  171.           hMainWnd,  NULL,
  172.           hInstance, NULL);
  173.       UpdateWindow(hInfoWnd);    
  174.     
  175.       /* create cursors */
  176.       ArrowCursor = LoadCursor(NULL, IDC_ARROW);
  177.       WaitCursor = LoadCursor(NULL, IDC_WAIT);
  178.       SetCursor(ArrowCursor);
  179. #ifdef WIN32
  180. #else
  181.       /* create thunk for Win16 */
  182.       lpfnConnectDlgProc = MakeProcInstance(ConnectDlgProc,hInstance);
  183.       lpfnFilesDlgProc = MakeProcInstance(FilesDlgProc,hInstance);
  184. #endif
  185.       /* gray out "Files" on menu bar */
  186.       hMenu = GetMenu(hMainWnd);
  187.       EnableMenuItem(hMenu,MSG_FILES,MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
  188.       DrawMenuBar(hMainWnd);
  189.       /* attach FCE */
  190.       Code = fceAttach(1);
  191.       if(Code<0) 
  192.         {Message("fceAttach fails");
  193.          break;
  194.         }
  195.       /* define LOG file */      
  196.       *Temp = '\0';
  197.       /* get FCE version */
  198.       Version = (int) fceGetInteger(0,FCE_GET_VERSION);      
  199.       break;
  200.  
  201.  
  202.     case WM_PAINT:  
  203.       hDC = BeginPaint(hMainWnd, &ps);
  204.       //SetMapMode(hDC,MM_ANISOTROPIC);
  205.       //SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  206.          
  207.  #ifdef WIN32        
  208.       wsprintf((LPSTR)Temp,"FCE32 Version %1d.%1d.%1d",
  209.         0x0f&(Version>>8),0x0f&(Version>>4),0x0f&Version);
  210. #else
  211.       wsprintf((LPSTR)Temp,"FCE16 Version %1d.%1d.%1d",
  212.         0x0f&(Version>>8),0x0f&(Version>>4),0x0f&Version);
  213. #endif        
  214.       TextOut(hDC,5, 5,(LPSTR)Temp,lstrlen(Temp));
  215.       TextOut(hDC,15,25,(LPSTR)Msg1,lstrlen(Msg1));
  216.       TextOut(hDC,15,40,(LPSTR)Msg2,lstrlen(Msg2));
  217.       TextOut(hDC,15,55,(LPSTR)Msg3,lstrlen(Msg3));
  218.       EndPaint(hMainWnd,&ps); 
  219.       break;
  220.    
  221.     
  222.     case WM_COMMAND:
  223.       switch(wParam)
  224.         {
  225.          case MSG_CONNECT:
  226. #ifdef WIN32
  227.            DialogBox(hInstance, "ConnectBox", hMainWnd, ConnectDlgProc);
  228. #else
  229.            DialogBox(hInstance, "ConnectBox", hMainWnd, lpfnConnectDlgProc);
  230. #endif
  231.            /* enable "files" if connected */
  232.            if(ConnectStatus)
  233.              {hMenu = GetMenu(hMainWnd);
  234.               EnableMenuItem(hMenu,MSG_FILES,MF_BYCOMMAND|MF_ENABLED);
  235.               EnableMenuItem(hMenu,MSG_CONNECT,MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);             
  236.               DrawMenuBar(hMainWnd);
  237.              }
  238.            return 0;
  239.            
  240.          case MSG_FILES:
  241. #ifdef WIN32
  242.            DialogBox(hInstance, "FilesBox", hMainWnd, FilesDlgProc);
  243. #else
  244.            DialogBox(hInstance, "FilesBox", hMainWnd, lpfnFilesDlgProc);
  245. #endif
  246.            if(!ConnectStatus)
  247.              {hMenu = GetMenu(hMainWnd);
  248.               EnableMenuItem(hMenu,MSG_CONNECT,MF_BYCOMMAND|MF_ENABLED);
  249.               EnableMenuItem(hMenu,MSG_FILES,MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);             
  250.               DrawMenuBar(hMainWnd);
  251.              }
  252.            return 0; 
  253.               
  254.          case MSG_EXIT:
  255.            if(ConnectStatus) fceClose(0);
  256.            fceRelease();
  257.            DestroyWindow(hMainWnd);
  258.            break;
  259.         }
  260.       break;     
  261.  
  262.     case WM_DESTROY:
  263.       PostQuitMessage(0);
  264.       break;
  265.       
  266.     default:
  267.       return (DefWindowProc(hMainWnd, iMsg, wParam, lParam));
  268.    }
  269.  return 0;
  270. }
  271.  
  272.